package com.yc.booktable.web;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.booktable.biz.BizException;
import com.yc.booktable.biz.FoodBiz;
import com.yc.booktable.entity.AdminUser;
import com.yc.booktable.entity.Food;
import com.yc.booktable.entity.Foodtype;
import com.yc.booktable.mapper.FoodMapper;
import com.yc.booktable.mapper.FoodtypeMapper;
import com.yc.booktable.service.impl.FoodServiceImpl;
import com.yc.booktable.service.impl.FoodtypeServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class FoodAction {
    @Resource
    private FoodMapper foodMapper;
    @Resource
    private FoodtypeMapper foodtypeMapper;
    @Resource
    private FoodServiceImpl foodService;
    @Resource
    private FoodtypeServiceImpl foodtypeService;
    @Resource
    private FoodBiz foodBiz;

    @RequestMapping("table")
    public String food(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer PageNum, HttpSession session){
        AdminUser user = (AdminUser) session.getAttribute("loginedUser");
        if(user==null|| user.getState()==1){
            return "redirect:/login";
        }
        model.addAttribute("user",user);
        Page page = new Page(PageNum, 10);
        page = foodMapper.selectPage(page,null);
        //List<Food> foods =foodMapper.selectList(null);
        model.addAttribute("food",page);
        List<Foodtype> foodtypes = foodtypeMapper.selectList(null);
        model.addAttribute("foodtype",foodtypes);
        return "table";
    }

    /**
     * 删除
     * @param fid
     * @param tid
     * @return
     */
    @RequestMapping({"/deletef/{fid}","/deleteft/{tid}"})
    public String move(@PathVariable(required = false) Integer fid,@PathVariable(required = false) Integer tid){
        try {
            System.out.println("删除");
            if (String.valueOf(fid)!="null") {
                foodService.removeById(fid);
            }else if(String.valueOf(tid)!="null"){

            foodtypeService.removeById(tid);
            }
            return "redirect:/table";
        } catch ( Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    /**
     * 添加食物种类
     * @param model
     * @return
     */
    @RequestMapping("notifications")
    public String ftInpt(Model model,HttpSession session){
        AdminUser user = (AdminUser) session.getAttribute("loginedUser");
        if(user==null|| user.getState()==1){
            return "redirect:/login";
        }
        model.addAttribute("user",user);
        model.addAttribute("ft",new Foodtype());
        model.addAttribute("f",new Food());
        List<Foodtype> foodtypes = foodtypeMapper.selectList(null);
        model.addAttribute("foodtype",foodtypes);
        /*System.out.println(foodtypes);*/
        return "notifications";
    }
    @PostMapping("/addft")
    public String add(Foodtype ft){
        foodtypeService.save(ft);
        return "redirect:/table";
    }
    /**
     * 添加菜品
     * @param
     * @return
     */
    @PostMapping("/addf")
    public String addf(Food f,HttpSession session){
        try {
            foodBiz.savef(f);
            return "redirect:/table";
        } catch (BizException e) {
            e.printStackTrace();
            session.setAttribute("msg",e.getMessage());
            return "forward:/error";
        }
    }
    /**
     * 修改食品和种类信息
     */
    @GetMapping("/queryf")
    public String queryf(Integer id,Model model){
        Food food1 = foodService.getById(id);
        model.addAttribute("food1",food1);
        return "editfood";
    }
    @PostMapping("updatef")
    public String update(Food food1,HttpSession session){
        try {
            foodBiz.savef(food1);
            return "redirect:/table";
        } catch (BizException e) {
            e.printStackTrace();
            session.setAttribute("msg",e.getMessage());
            return "forward:/error1";
        }
    }
    @GetMapping("/queryft")
    public String queryft(Integer id,Model model){
        Foodtype ft1 = foodtypeService.getById(id);
        model.addAttribute("ft1",ft1);
        return "editft";
    }
    @PostMapping("updateft")
    public String update1(Foodtype ft1){
        foodtypeService.saveOrUpdate(ft1);
        return "redirect:/table";
    }
}
